package org.example.lottery_system.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import org.example.lottery_system.common.exception.ControllerException;
import org.example.lottery_system.common.pojo.CommonResult;
import org.example.lottery_system.common.utils.JWTUtil;
import org.example.lottery_system.common.utils.JacksonUtil;
import org.example.lottery_system.controller.param.UserPasswordLoginParam;
import org.example.lottery_system.controller.param.UserRegisterParam;
import org.example.lottery_system.controller.result.UserLoginResult;
import org.example.lottery_system.controller.result.UserRegistResult;
import org.example.lottery_system.service.dto.UserLoginDto;
import org.example.lottery_system.service.dto.UserRegisterDto;
import org.example.lottery_system.service.impl.UserServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("user")
public class UserController {
//    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserServiceImpl userService;
    @PostMapping("/register")
    public CommonResult<UserRegistResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        //这个{}是一个占位符会被后面的参数替换
        //打印日志
        log.info("占位符{}", JacksonUtil.writeValueAsString(param));
        System.out.println(param.toString());
        //调用service层方法
        UserRegisterDto userRegisterDto = userService.userRegister(param);
        System.out.println(userRegisterDto.getUserId());
        return CommonResult.success(converToUserRegisterResult(userRegisterDto));
    }

    @PostMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@Validated @RequestBody UserPasswordLoginParam param){
        log.info(JacksonUtil.writeValueAsString(param));
        UserLoginDto login = userService.login(param);
        return CommonResult.success(ConverToUserLoginResult(login));

    }

    private UserLoginResult ConverToUserLoginResult(UserLoginDto param) {
        if(param == null){
            throw new ControllerException(ControllerErrorCodeConstants.Login_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(param.getToken());
        userLoginResult.setIdentity(param.getIdentity().name());
        return userLoginResult;
    }

    private UserRegistResult converToUserRegisterResult(UserRegisterDto userRegisterDto) {
        if (null == userRegisterDto){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        UserRegistResult userRegistResult = new UserRegistResult();
        userRegistResult.setId(userRegisterDto.getUserId());
        return userRegistResult;
    }
}
